package com.graduation.mathematics.base;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.graduation.mathematics.util.UiUtil;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @ProjectName: botanyEncyclopedia
 * @Package: com.graduation.botanyencyclopedia.base.ui
 * @ClassName: BaseFragment
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/2/5 15:10
 * @Version: 1.0
 */
public abstract class BaseFragment extends SupportFragment {
    protected View mView;
    private boolean mIsInited; // 懒加载使用
    protected LoadingDialog mLoadingDialog;
    private Unbinder bind;

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView =inflater.inflate(getLayoutId(), container, false);
        bind = ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!isHidden()) {
            mIsInited = true;
            initEventAndData();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!mIsInited && !isHidden()) {
            mIsInited = true;
            initEventAndData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLoadingDialog != null) {
            mLoadingDialog.close();
        }
        mView = null;
    }

    public void showLoadingDialog(final String str) {
        UiUtil.post(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog != null) {
                    mLoadingDialog.close();
                }
                mLoadingDialog = null;
                if (!isAdded() || isDetached() || isRemoving() || getContext() == null) {
                    return;
                }
                mLoadingDialog = new LoadingDialog(getContext());
                if (!TextUtils.isEmpty(str)) {
                    mLoadingDialog.setLoadingText(str);
                }
                mLoadingDialog.show();
            }
        });
    }

    public void updateLoadingDialog(final String str) {
        UiUtil.post(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog == null) {
                    return;
                }
                if (!isAdded() || isDetached() || isRemoving() || getContext() == null) {
                    return;
                }
                if (!TextUtils.isEmpty(str)) {
                    mLoadingDialog.setLoadingText(str);
                }
            }
        });
    }

    public void closeLoadingDialog() {
        UiUtil.post(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog != null) {
                    mLoadingDialog.close();
                }
            }
        });
    }

    public View getLayoutView() {
        return mView;
    }

}
